package com.basic.message.handler;

import com.basic.dto.CommentDTO;
import com.basic.dto.DynamicDTO;
import com.basic.dto.InformDTO;
import com.basic.enums.EntityTypeEnum;
import com.basic.mapper.CommentMapper;
import com.basic.mapper.DynamicMapper;
import com.basic.message.AsyncMessageDTO;
import com.basic.message.MessageManage;
import com.basic.message.MessageTypeEnum;
import com.basic.message.dto.CommentMessageDTO;
import com.basic.util.CommonUtil;
import com.basic.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommentMessageHandler implements MessageManage {

    final static Logger logger = LoggerFactory.getLogger(CommentMessageHandler.class);

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private DynamicMapper dynamicMapper;

    @Override
    public void dealMessage(AsyncMessageDTO asyncMessageDTO) {
        logger.info("开始处理评论产生的消息:{}", JsonUtils.objectToJson(asyncMessageDTO));

        String message = asyncMessageDTO.getMessage();
        //保证DTO的责任单一性，防止被其他人篡改
        CommentMessageDTO commentMessageDTO = JsonUtils.jsonToPojo(message, CommentMessageDTO.class);
        //获取评论主体
        CommentDTO commentDTO = commentMapper.queryCommentDTOById(commentMessageDTO.getId());

        /**
         * TODO 1、判断是否是对动态进行评论，动态的评论数量增加；
         * TODO 2、判断是否是对评论进行回复，如果是，需要通知被回复的人；同时通知动态所属用户有新的评论
         * TODO 作业：1、设置消息表；2、设计消息相关的接口;3、将当前消息处理一下
         */

        //更新动态评论数量
        DynamicDTO dynamicDTO = dynamicMapper.queryDynamicById(commentDTO.getDynamicId());
        dynamicDTO.setCommentCount(dynamicDTO.getCommentCount() + 1);
        dynamicMapper.updateDynamicById(dynamicDTO);


        /**
         * TODO 消息主体可以添加状态--已读/未读
         */

        //获取该评论针对的实体类型
        Integer entityType = commentDTO.getEntityType();

        if(EntityTypeEnum.DYNAMIC.getType().equals(entityType)) {
            //消息通知
            InformDTO informDTO = new InformDTO();
            informDTO.setId(CommonUtil.createUUID());
            informDTO.setSendUserId(commentDTO.getUserId());
            informDTO.setTakeUserId(dynamicDTO.getUserId());
            informDTO.setEntityId(dynamicDTO.getId());
            informDTO.setEntityType(EntityTypeEnum.DYNAMIC.getType());
            informDTO.setContent("评论内容: " + commentDTO.getContent());

            //保存 -- insert

            return;
        }


        if(EntityTypeEnum.COMMENT.getType().equals(entityType)) {
            CommentDTO commentTemp = commentMapper.queryCommentDTOById(commentDTO.getEntityId());

            //通知被评论的主体所属用户
            InformDTO informDTO = new InformDTO();
            informDTO.setId(CommonUtil.createUUID());
            informDTO.setSendUserId(commentDTO.getUserId());
            informDTO.setTakeUserId(commentTemp.getUserId());
            informDTO.setEntityId(commentTemp.getId());
            informDTO.setEntityType(EntityTypeEnum.COMMENT.getType());
            informDTO.setContent("评论内容: " + commentDTO.getContent());
            //保存--insert

            //通知被评论的动态所属用户
            informDTO.setId(CommonUtil.createUUID());
            informDTO.setSendUserId(commentDTO.getUserId());
            informDTO.setTakeUserId(dynamicDTO.getUserId());
            informDTO.setEntityId(dynamicDTO.getId());
            informDTO.setEntityType(EntityTypeEnum.DYNAMIC.getType());
            informDTO.setContent("评论内容: " + commentDTO.getContent());
            //保存--insert

        }

    }

    @Override
    public String getMessageType() {
        return MessageTypeEnum.COMMENT_MESSAGE.getType();
    }

}
